package com.innjia.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.http.HttpUtil;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;

/**
 * <br>
 * 2017-11-27
 *
 * @author zhouxianglh@gmail.com
 */
public class MyLog extends DailyRollingFileAppender {
    @Override
    protected void subAppend(LoggingEvent event) {
        super.subAppend(event);
        String messageStr = this.layout.format(event);
        Message message = new Message();
        Message.Content content = new Message.Content(messageStr);
        message.setText(content);
        System.out.println(JSONObject.toJSONString(message));
        HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=a16fa59cc20ec30c0c3d9243b9a7e8bd8dd78d7683864f827adacdfe5e951453", JSONObject.toJSONString(message));
    }

    static class Message {
        private String msgtype = "text";

        private Content text;

        public String getMsgtype() {
            return msgtype;
        }

        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }

        public Content getText() {
            return text;
        }

        public void setText(Content text) {
            this.text = text;
        }

        static class Content {
            String content;

            public Content(String content) {
                this.content = content;
            }

            public String getContent() {
                return content;
            }

            public void setContent(String content) {
                this.content = content;
            }
        }
    }
}
